import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
//import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
// import { getWeekMonthQuarterYear } from '/@/utils';
import { useGlobSetting } from '/@/hooks/setting';
import { filterDictTextByCache } from '/@/utils/dict/JDictSelectUtil';
import { h } from 'vue';

import { sjlyOptionsListApi } from '/@/api/wyf/pms/select';
import { xzqhTreeSelectListApi } from '/@/api/wyf/xzqh/select';
import { gxqyOptionsListApi } from '@/api/wyf/user/gxqySelect';

const globSetting = useGlobSetting();
const baseUploadUrl = globSetting.uploadUrl;
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '房源编号',
    align: 'left',
    dataIndex: 'wyfbh',
    //resizable: true,
  },
  {
    title: '房源名称',
    align: 'left',
    dataIndex: 'fh',
    //resizable: true,
  },
  {
    title: '管辖单位',
    align: 'left',
    dataIndex: 'gxdwmc',
    // resizable: true,
  },
  {
    title: '所属区域',
    align: 'left',
    dataIndex: 'xzqh_dictText',
    // resizable: true,
  },
  {
    title: '详细地址',
    align: 'left',
    dataIndex: 'fwdz',
    width: 200,
    // resizable: true,
  },
  {
    title: '经营类型',
    align: 'left',
    dataIndex: 'jyrlx',
    customRender: ({ text }) => {
      return render.renderDict(text, 'jyrlx');
    },
  },
  {
    title: '经营者',
    align: 'left',
    dataIndex: 'jyrxm',
    customRender: ({ record }) => {
      const jyrlx = record['jyrlx'];
      if (jyrlx === '1' || jyrlx === '3') {
        return record['jyrxm']; // 当 jyrlx 为 1 时，显示 jyrxm
      } else if (jyrlx === '2') {
        return record['jyrdwmc']; // 当 jyrlx 为 2 时，显示 jyrdwmc
      }
      return ''; // 默认返回空
    },
  },
  {
    title: '经营状态',
    align: 'left',
    dataIndex: 'fwjyzt',
    customRender: ({ text }) => {
      const color = text == '0' ? 'green' : text == '1' ? 'default' : 'red';
      return render.renderTag(render.renderDict(text, 'fwjyzt'), color);
    },
  },
  {
    title: '数据来源',
    align: 'left',
    dataIndex: 'sjly_dictText',
    width: 170,
    // resizable: true,
  },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  // {
  //   field: 'gxqy',
  //   label: '管辖区域',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: gxqyOptionsListApi,
  //     resultField: 'result',
  //     labelField: 'id_dictText',
  //     valueField: 'id',
  //     mode: 'multiple',
  //   },
  // },

  {
    label: '房源名称',
    field: 'fh',
    component: 'Input', //渲染的组件
    componentProps: {
      //  dictCode: 'status',
    },
  },
  {
    field: 'gxqy',
    label: '管辖单位',
    //component: 'ApiTreeSelect',
    component: 'JCascader',
    componentProps: {
      api: gxqyOptionsListApi,
      params: {},
      //resultField: 'title',
      // fieldNames: {
      //   label: 'id_dictText',
      //   value: 'id',
      // },
      // multiple: true,
      changeOnSelect: true,
    },
  },
  //{
  //   field: 'xzqh',
  //  label: '所属区域',
  //   component: 'ApiTreeSelect',
  //   componentProps: {
  //     api: xzqhTreeSelectListApi,
  //     params: {},
  //     resultField: 'result',
  //     multiple: true,
  //   },
  //  },
  {
    field: 'xzqh',
    label: '所属区域',
    //component: 'ApiTreeSelect',
    component: 'JCascader',
    componentProps: {
      api: xzqhTreeSelectListApi,
      params: {},
      //resultField: 'title',
      // fieldNames: {
      //   label: 'title',
      //   value: 'value',
      // },
      // multiple: true,
      changeOnSelect: true,
    },
  },
  {
    label: '房源编号',
    field: 'wyfbh',
    component: 'Input', //渲染的组件
    componentProps: {
      //  dictCode: 'status',
    },
  },
  {
    label: '详细地址',
    field: 'fwdz',
    component: 'Input', //渲染的组件
  },
  {
    label: '数据来源',
    field: 'sjly',
    component: 'ApiSelect', //渲染的组件
    //defaultValue: '', //设置默认值
    componentProps: {
      api: sjlyOptionsListApi,
      resultField: 'result',
      labelField: 'mc',
      valueField: 'id',
    },
  },
  {
    label: '经营类型',
    field: 'jyrlx',
    component: 'JDictSelectTag', //渲染的组件
    //defaultValue: '', //设置默认值
    componentProps: {
      dictCode: 'jyrlx',
    },
  },
  {
    label: '经营者',
    field: 'jyrxm',
    component: 'Input', //渲染的组件
  },
  {
    label: '经营状态',
    field: 'fwjyzt',
    component: 'JDictSelectTag', //渲染的组件
    //defaultValue: '', //设置默认值
    componentProps: {
      dictCode: 'fwjyzt',
    },
  },
];
/**
 * 上传父路径
 */
export const uploadUrl = `${baseUploadUrl}/wyf/common/upload`;
//表单数据
export const formSchema: FormSchema[] = [
  {
    field: '基础信息',
    component: 'Input',
    label: '',
    slot: 'title1',
  },
  {
    label: '房源编号',
    field: 'wyfbh',
    required: true,
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      return !!values.id;
    },
  },
  // {
  //   field: 'xzqh',
  //   label: '所属区域：',
  //   component: 'ApiTreeSelect',
  //   componentProps: {
  //     api: xzqhTreeSelectListApi,
  //     params: {},
  //     resultField: 'result',
  //     //  multiple: true,
  //   },
  //   required: true,
  // },
  {
    field: 'xzqh',
    label: '所属区域',
    component: 'JCascader',
    required: true,
    componentProps: {
      api: xzqhTreeSelectListApi,
      params: {},
      //resultField: 'title',
      // fieldNames: {
      //   label: 'title',
      //   value: 'value',
      // },
      // multiple: true,
      //changeOnSelect: true,
    },
  },
  {
    label: '房屋地址',
    field: 'fwdz',
    required: true,
    component: 'Input',
  },
  {
    label: '房源名称',
    field: 'fh',
    required: true,
    component: 'Input',
  },
  {
    label: '营业信息',
    field: 'fwjyzt',
    required: true,
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'fwjyzt',
    },
    // render: ({ schema, values, model, field }) => {
    //   const color = values.fwjyzt == '1' ? 'blue' : values.fwjyzt == 'M' ? 'yellow' : 'red';
    //   return render.renderTag(33, color);
    // },
    // required: true,
  },
  {
    label: '数据来源',
    field: 'sjly_dictText',
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      return true;
    },
  },
  {
    field: '经营人信息',
    component: 'Input',
    label: '',
    slot: 'title2',
  },
  {
    label: '经营类型',
    field: 'jyrlx',
    required: true,
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'jyrlx',
    },
  },
  {
    label: '单位名称',
    field: 'jyrdwmc',
    required: true,
    component: 'Input',
    ifShow: ({ values }) => values.jyrlx == 2,
  },
  {
    label: '信用代码',
    field: 'tyshxydm',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => values.jyrlx == 2,
  },
  {
    label: '姓名',
    field: 'jyrxm',
    required: true,
    component: 'Input',
    ifShow: ({ values }) => values.jyrlx != 2,
  },
  {
    label: '身份证号',
    field: 'jyrzjhm',
    required: true,
    component: 'Input',
    dynamicRules: ({ model, schema }) => {
      return [
        // { ...rules.duplicateCheckRule('sys_user', 'phone', model, schema, true)[0], trigger: 'blur' },
        {
          pattern: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,
          message: '身份证格式有误',
          trigger: 'blur',
        },
      ];
    },
    ifShow: ({ values }) => values.jyrlx != 2,
  },
  {
    label: '联系方式',
    field: 'jyrlxdh',
    // required: true,
    component: 'Input',
    dynamicRules: ({ model, schema }) => {
      return [
        // { ...rules.duplicateCheckRule('sys_user', 'phone', model, schema, true)[0], trigger: 'blur' },
        { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式有误', trigger: 'blur' },
      ];
    },
    ifShow: ({ values }) => values.jyrlx != 2,
  },
  {
    label: '',
    field: '产权人信息',
    component: 'Input',
    slot: 'title3',
  },
  {
    label: '产权类型',
    field: 'cqrlx',
    required: false,
    component: 'JDictSelectTag',
    // required: true,
    componentProps: {
      dictCode: 'cqrlx',
    },
  },
  {
    label: '单位名称',
    field: 'cqrdwmc',
    required: false,
    component: 'Input',
    ifShow: ({ values }) => values.cqrlx != 1,
  },
  {
    label: '信用代码',
    field: 'cqrtyshxydm',
    component: 'Input',
    required: false,
    ifShow: ({ values }) => values.cqrlx != 1,
  },
  {
    label: '姓名',
    field: 'cqrxm',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => values.cqrlx == 1,
  },
  {
    label: '身份证号',
    field: 'cqrzjhm',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => values.cqrlx == 1,
    dynamicRules: ({ model, schema }) => {
      return [
        // { ...rules.duplicateCheckRule('sys_user', 'phone', model, schema, true)[0], trigger: 'blur' },
        {
          pattern: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,
          message: '身份证格式有误',
          trigger: 'blur',
        },
      ];
    },
  },
  {
    label: '联系电话',
    field: 'cqrlxdh',
    component: 'Input',
    // required: true,
    ifShow: ({ values }) => values.cqrlx == 1,
  },
  {
    label: '产权证',
    field: 'fjlx_4',
    component: 'JImageUpload',
    componentProps: ({ formActionType, formModel }) => {
      return {
        sync: false,
        fileMax: 4,
        data: {
          fjlx: 4,
        },
        actionUrl: uploadUrl,
        onChange: (urls: string, ids: string) => {
          if (!urls) {
            formModel.fjlx_4_ids = '';
            return;
          }
          if (urls && ids) {
            formModel.fjlx_4_ids = ids;
          }
        },
      };
    },
  },
  {
    label: '产权证附件Ids',
    field: 'fjlx_4_ids',
    component: 'Input',
    show: false,
  },
  {
    label: '户型图',
    field: 'fjlx_5',
    component: 'JImageUpload',
    componentProps: ({ formActionType, formModel }) => {
      return {
        sync: false,
        fileMax: 4,
        data: {
          fjlx: 5,
        },
        actionUrl: uploadUrl,
        onChange: (urls: string, ids: string) => {
          if (!urls) {
            formModel.fjlx_5_ids = '';
            return;
          }
          if (urls && ids) {
            formModel.fjlx_5_ids = ids;
          }
        },
      };
    },
  },
  {
    label: '户型图附件Ids',
    field: 'fjlx_5_ids',
    component: 'Input',
    show: false,
  },
  {
    label: '租赁合同',
    field: 'fjlx_6',
    component: 'JImageUpload',
    componentProps: ({ formActionType, formModel }) => {
      return {
        sync: false,
        fileMax: 4,
        data: {
          fjlx: 6,
        },
        actionUrl: uploadUrl,
        onChange: (urls: string, ids: string) => {
          console.log(urls, ids, 2222222222);
          if (!urls) {
            formModel.fjlx_6_ids = '';
            return;
          }
          if (urls && ids) {
            formModel.fjlx_6_ids = ids;
          }
        },
      };
    },
  },
  {
    label: '租赁合同附件Ids',
    field: 'fjlx_6_ids',
    component: 'Input',
    show: false,
  },
  {
    label: '现场照片',
    field: 'fjlx_2',
    component: 'JImageUpload',
    componentProps: ({ formActionType, formModel }) => {
      return {
        sync: false,
        fileMax: 4,
        data: {
          fjlx: 2,
        },
        actionUrl: uploadUrl,
        onChange: (urls: string, ids: string) => {
          if (!urls) {
            formModel.fjlx_2_ids = '';
            return;
          }
          if (urls && ids) {
            formModel.fjlx_2_ids = ids;
          }
        },
      };
    },
  },
  {
    label: '现场照片附件Ids',
    field: 'fjlx_2_ids',
    component: 'Input',
    show: false,
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
];
export const detailSchema = [
  {
    field: '审核记录',
    component: 'Input',
    label: '',
    slot: 'title4',
  },
  {
    label: '审核结果',
    field: 'shbz',
    component: 'Input',
    render: (text) => {
      return filterDictTextByCache('shbz', text);
    },
    color: (text) => {
      return text == 1 ? 'text-green' : text == 2 ? 'text-red' : 'text-waring';
    },
  },
  {
    label: '审核意见',
    field: 'shyj',
    component: 'Input',
  },
  {
    label: '审核时间',
    field: 'createTime',
    component: 'Input',
  },
  {
    label: '审核人',
    field: 'shrxm',
    component: 'Input',
  },

  {
    field: '基础信息',
    component: 'Input',
    label: '',
    slot: 'title1',
  },
  {
    label: '房源编号',
    field: 'wyfbh',
    component: 'Input',
  },
  {
    label: '所属区域',
    field: 'xzqh',
    component: 'ApiTreeSelect',
    componentProps: {
      saveCode: 'region',
      placeholder: '请选择',
    },
  },
  {
    label: '房屋地址',
    field: 'fwdz',
    component: 'Input',
  },
  {
    label: '房源名称',
    field: 'fh',
    component: 'Input',
  },
  {
    label: '数据来源',
    field: 'sjly_dictText',
    component: 'Input',
  },
  {
    label: '上报时间',
    field: 'createTime',
    component: 'Input',
  },
  {
    label: '营业信息',
    field: 'fwjyzt',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'fwjyzt',
    },
    render: (text) => {
      console.log(text, '字典');
      return filterDictTextByCache('fwjyzt', text);
    },
    color: (text) => {
      return text == 0 ? 'text-green' : text == 2 ? 'text-red' : 'text-waring';
    },
  },
  {
    field: '经营人信息',
    component: 'Input',
    label: '',
    slot: 'title2',
  },
  {
    label: '经营类型',
    field: 'jyrlx',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'jyrlx',
    },
    render: (text) => {
      return filterDictTextByCache('jyrlx', text);
    },
  },
  {
    label: '单位名称',
    field: 'jyrdwmc',
    ifShow: ({ values }) => values.jyrlx == 2,
  },
  {
    label: '信用代码',
    field: 'tyshxydm',
    ifShow: ({ values }) => values.jyrlx == 2,
  },
  {
    label: '姓名',
    field: 'jyrxm',
    component: 'Input',
    //ifShow: ({ values }) => values.jyrlx != 2,
  },
  {
    label: '身份证号',
    field: 'jyrzjhm',
    component: 'Input',
    //ifShow: ({ values }) => values.jyrlx != 2,
  },
  {
    label: '联系方式',
    field: 'jyrlxdh',
    component: 'Input',
    //ifShow: ({ values }) => values.jyrlx != 2,
  },
  {
    label: '',
    field: '产权人信息',
    component: 'Input',
    slot: 'title3',
  },
  {
    label: '产权类型',
    field: 'cqrlx',
    component: 'Input',
    render: (text) => {
      return filterDictTextByCache('cqrlx', text);
    },
  },
  {
    label: '单位名称',
    field: 'cqrdwmc',
    component: 'Input',
    ifShow: ({ values }) => values.cqrlx == 2,
  },
  {
    label: '信用代码',
    field: 'tyshxydm',
    component: 'Input',
    ifShow: ({ values }) => values.cqrlx == 2,
  },
  {
    label: '姓名',
    field: 'cqrxm',
    component: 'Input',
    //ifShow: ({ values }) => values.cqrlx == 1,
  },
  {
    label: '身份证号',
    field: 'cqrzjhm',
    component: 'Input',
    //ifShow: ({ values }) => values.cqrlx == 1,
  },
  {
    label: '联系电话',
    field: 'cqrlxdh',
    component: 'Input',
    //ifShow: ({ values }) => values.cqrlx == 1,
  },
  {
    label: '产权证',
    field: 'fjlx_4',
    component: 'JImageUpload',
  },
  {
    label: '户型图',
    field: 'fjlx_5',
    component: 'JImageUpload',
  },
  {
    label: '租赁合同',
    field: 'fjlx_6',
    component: 'JImageUpload',
  },
  {
    label: '现场照片',
    field: 'fjlx_2',
    component: 'JImageUpload',
  },
  {
    field: '入住记录1',
    component: 'Input',
    label: '',
    slot: 'title5',
    type: 'card',
  },
];
/**
 * 流程表单调用这个方法获取formSchema
 * @param param
 */
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
//入住列表数据
export const historyColumns: BasicColumn[] = [
  {
    title: '住客信息',
    align: 'left',
    dataIndex: 'sjly',
    width: 160,
    customRender: ({ text, record }) => {
      let xm, zjhm, lxdh, rzbz;
      if (record.tenantsList && record.tenantsList.length > 0) {
        xm = record.tenantsList[0].xm || '';
        zjhm = record.tenantsList[0].zjhm || '';
        lxdh = record.tenantsList[0].lxdh || '';
        rzbz = record.tenantsList[0].rzbz || '';
      }
      const rzbzText = filterDictTextByCache('rzbz', rzbz);
      const color = rzbz == '1' || rzbz == '3' ? 'text-green' : rzbz == '5' ? 'text-red' : 'text-waring';
      return h('div', { class: 'zkxx' }, [
        h('div', null, `${xm}`),
        h('div', null, `${zjhm}`),
        h('div', null, `${lxdh}`),
        h('div', { class: color }, `${rzbzText}`),
      ]);
    },
    //resizable: true,
  },
  {
    title: '预定入离时间',
    align: 'left',
    dataIndex: 'ddjssj',
    width: 160,
    customRender: ({ text, record }) => {
      return `${record.ddkssj ?? ''}~${record.ddjssj ?? ''}`;
    },
  },
  {
    title: '实际居住时间',
    align: 'left',
    dataIndex: 'sjrzsj',
    width: 160,
    customRender: ({ text, record }) => {
      return `${record.sjrzsj ?? ''}~${record.sjldsj ?? ''}`;
    },
  },
];
//抓拍列表数据
export const captureColumns: BasicColumn[] = [
  {
    title: '抓拍时间',
    align: 'left',
    dataIndex: 'zpsj',
    width: 160,
  },
  {
    title: '抓拍照片',
    align: 'left',
    dataIndex: 'zpzpids',
    width: 120,
    slots: { customRender: 'zpzpids' },
  },
  // {
  //   title: '抓拍照片',
  //   field: 'zpzpids',
  //   component: 'JImageUpload',
  //   componentProps: ({ formActionType, formModel }) => {
  //     return {
  //       sync: false,
  //       fileMax: 4,
  //       data: {
  //         fjlx: 2,
  //       },
  //       actionUrl: uploadUrl,
  //       onChange: (urls: string, ids: string) => {
  //         if (!urls) {
  //           formModel.fjlx_2_ids = '';
  //           return;
  //         }
  //         if (urls && ids) {
  //           formModel.fjlx_2_ids = ids;
  //         }
  //       },
  //     };
  //   },
  // },
  // {
  //   title: '识别状态',
  //   align: 'left',
  //   dataIndex: 'sbStatus',
  //   width: 100,
  //   customRender: ({ text }) => {
  //     return filterDictTextByCache('sbStatus', text);
  //   },
  // },
];
//审核数据
export const approvedSchema: FormSchema[] = [
  {
    label: '',
    field: 'shyj',
    component: 'InputTextArea',
  },
  {
    label: '',
    field: 'houseId',
    component: 'Input',
    show: false,
  },
];
